//
//  transition.m
//  EFExtend
//
//  Created by mac on 15/3/26.
//  Copyright (c) 2015年 Eric. All rights reserved.
//

#import "transition.h"


@interface transition ()

@end

@implementation transition{
    NSMutableArray *boards;
}


- (void)dealloc
{
   
    HM_SUPER_DEALLOC();
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    FUNC;
}


- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    
    [self.customNavLeftBtn setImage:[UIImage imageNamed:@"back.png"] forState:UIControlStateNormal];
    [self.customNavLeftBtn setFrame:CGRectMakeBound(32, 32)];
    
    boards = [[NSMutableArray alloc]init];
    
    [boards addObject:@{@"title":@"open 方式切换",@"subtitle":@"UIViewAnimationOptionCurveEaseInOut",@"option":@(UIViewAnimationOptionCurveEaseInOut),@"mode":@"open",@"map":URLFOR_controllerWithNav(@"transitionAnimater")}];
    
    [boards addObject:@{@"title":@"open 方式切换",@"subtitle":@"UIViewAnimationOptionCustom",@"option":@(UIViewAnimationOptionCustom),@"mode":@"open",@"map":URLFOR_controllerWithNav(@"transitionAnimater")}];
    
    [boards addObject:@{@"title":@"open 方式切换",@"subtitle":@"从上往下",@"option":@(UIViewAnimationOptionTransitionFromTop),@"mode":@"open",@"map":URLFOR_controllerWithNav(@"transitionAnimater")}];
    [boards addObject:@{@"title":@"open 方式切换",@"subtitle":@"从下往上",@"option":@(UIViewAnimationOptionTransitionFromBottom),@"mode":@"open",@"map":URLFOR_controllerWithNav(@"transitionAnimater")}];
    [boards addObject:@{@"title":@"open 方式切换",@"subtitle":@"从左往右",@"option":@(UIViewAnimationOptionTransitionFromLeft),@"mode":@"open",@"map":URLFOR_controllerWithNav(@"transitionAnimater")}];
    [boards addObject:@{@"title":@"open 方式切换",@"subtitle":@"从右往左",@"option":@(UIViewAnimationOptionTransitionFromRight),@"mode":@"open",@"map":URLFOR_controllerWithNav(@"transitionAnimater")}];
    [boards addObject:@{@"title":@"open 方式切换",@"subtitle":@"从左往右翻转",@"option":@(UIViewAnimationOptionTransitionFlipFromLeft),@"mode":@"open",@"map":URLFOR_controllerWithNav(@"transitionAnimater")}];
    [boards addObject:@{@"title":@"open 方式切换",@"subtitle":@"从右往左翻转",@"option":@(UIViewAnimationOptionTransitionFlipFromRight),@"mode":@"open",@"map":URLFOR_controllerWithNav(@"transitionAnimater")}];
    [boards addObject:@{@"title":@"open 方式切换",@"subtitle":@"向下翻书",@"option":@(UIViewAnimationOptionTransitionCurlDown),@"mode":@"open",@"map":URLFOR_controllerWithNav(@"transitionAnimater")}];
    [boards addObject:@{@"title":@"open 方式切换",@"subtitle":@"向上翻书",@"option":@(UIViewAnimationOptionTransitionCurlUp),@"mode":@"open",@"map":URLFOR_controllerWithNav(@"transitionAnimater")}];
    [boards addObject:@{@"title":@"open 方式切换",@"subtitle":@"从上往下翻转",@"option":@(UIViewAnimationOptionTransitionFlipFromTop),@"mode":@"open",@"map":URLFOR_controllerWithNav(@"transitionAnimater")}];
    [boards addObject:@{@"title":@"open 方式切换",@"subtitle":@"从下往上翻转",@"option":@(UIViewAnimationOptionTransitionFlipFromBottom),@"mode":@"open",@"map":URLFOR_controllerWithNav(@"transitionAnimater")}];
    [boards addObject:@{@"title":@"open 方式切换",@"subtitle":@"打开原生页面",@"option":@(UIViewAnimationOptionCurveEaseInOut),@"mode":@"open",@"map":URLFOR_controllerWithNav(@"NativeVC")}];
    [boards addObject:@{@"title":@"present 方式切换",@"subtitle":@"打开原生页面",@"option":@(UIViewAnimationOptionCurveEaseInOut),@"mode":@"present",@"map":URLFOR_controllerWithNav(@"NativeVC")}];
    [boards addObject:@{@"title":@"push 方式切换",@"subtitle":@"打开原生页面",@"option":@(UIViewAnimationOptionCurveEaseInOut),@"mode":@"push",@"map":URLFOR_controller(@"NativeVC")}];
    [boards addObject:@{@"title":@"push 方式切换",@"subtitle":@"返回",@"option":@(UIViewAnimationOptionCurveEaseInOut),@"mode":@"pushtoroot",@"map":URLFOR_controller(@"NativeVC")}];
    [boards addObject:@{@"title":@"push 方式切换",@"subtitle":@"返回",@"option":@(UIViewAnimationOptionCurveEaseInOut),@"mode":@"push",@"map":URLFOR_controller(@"transitionAnimater")}];
    
    [boards addObject:@{@"title":@"present 方式切换",@"subtitle":@"返回",@"option":@(UIViewAnimationOptionCurveEaseInOut),@"mode":@"present",@"map":URLFOR_controllerWithNav(@"TestVC")}];
    
    [self.tableView reloadData];
    
    INFO(self.stack.boards);
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (BOOL) shouldAutorotateToInterfaceOrientation:
(UIInterfaceOrientation)toInterfaceOrientation {
    return (toInterfaceOrientation == UIInterfaceOrientationMaskPortrait);
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait;
}

ON_Button(signal){
    UIButten *btn = signal.source;
    if ([signal is:[UIButten TOUCH_UP_INSIDE]]) {
        if ([btn is:@"leftBtn"]) {//customNavLeftBtn
            [self backAndRemoveWithAnimate:YES];
        }else if ([btn is:@"rightBtn"]){//customNavRightBtn
            
        }
    }
}
#pragma  mark - table delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return boards.count;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier=@"listCell";
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier];
    
    if (!cell) {
        cell= [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
        cell.selectionStyle = UITableViewCellSelectionStyleGray;
        
    }
    NSDictionary *city = [boards objectAtIndex:indexPath.row];
    cell.textLabel.text = [city valueForKey:@"title"];
    cell.detailTextLabel.text = [city valueForKey:@"subtitle"];
    cell.detailTextLabel.textColor = RGB(.5, .5, .5);
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    NSDictionary *city = [boards objectAtIndex:indexPath.row];
    NSString *map = [city valueForKey:@"map"];
    NSString *title = [city valueForKey:@"title"];
    NSString *mode = [city valueForKey:@"mode"];
    NSNumber *option = [city valueForKey:@"option"];
    UIViewController *c = nil;
    if ([mode is:@"open"]) {
        HMURLMAPItem *item = [HMURLMap map:map bindto:@"*" URL:map];
        item.option = [option integerValue];
        if ( (UIViewAnimationOptions)UIViewAnimationOptionCustom == item.option) {
            item.animaterBack = [[AnimatorZBFallenBricks alloc]init];
            item.animaterFront = [[AnimatorTransitionHorizontalLines alloc]init];
        }
        
        c = [self open:map animate:YES];
    }else if ([mode is:@"pushtoroot"]){
        [self.navigationController popToRootViewControllerAnimated:YES];
    }else if ([mode is:@"present"]){
//        UINavigationController *nv = [[HMUIStack alloc]initWithRootViewController:[[NSClassFromString(@"NativeVC") alloc]init]];
//        HMUIBoard *mainBoard =  [HMURLMap boardForMap:@"*"];
//        [mainBoard presentViewController:nv animated:YES completion:^{
//            
//        }];
        HMUIBoard *mainBoard =  [HMURLMap boardForMap:@"*"];
        [[HMTicker sharedInstance] delayInMain:1.f invoke:^{
            UIViewController *vvv = [[HMUIStack alloc]initWithRootViewController:[[NSClassFromString(@"NativeVC") alloc]init]];
            [self presentViewController:vvv animated:YES completion:^{
                
            }];
        }];
        
//        [self presentToPath:map map:nil animated:YES];
    }else{
        c =[self pushToPath:map animated:YES];
    }
    if ([c isKindOfClass:[UINavigationController class]]) {
        [(UINavigationController*)c topViewController].title = title;
    }else{
        c.title = title;
    }
}

@end
